//
//  XHRSickBranchesModel.m
//  快医
//
//  Created by 孙博 on 16/7/8.
//
//

#import "XHRSickBranchesModel.h"
#import "XHRNetworkingToolManager.h"

/** 肿瘤疾病选择疾病细分列表接口 */
#define kSearchCI3Url @"http://iosapi.itcast.cn/searchCI3List.json.php"

@implementation XHRSickBranchesModel

+ (instancetype)modelWithDict:(NSDictionary *)dict{
    
    XHRSickBranchesModel *model = [XHRSickBranchesModel new];
    
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

//    请求地址: http://iosapi.itcast.cn/searchCI3List.json.php
//    请求参数:{"page":1,"page_size":15,"ci1_id":1,"keyword":""}
+ (void)getSickBranchesListSuccess:(void (^)(NSArray<XHRSickBranchesModel *> *))successBlock error:(void (^)())errorBlock{
    
    XHRNetworkingToolManager *manager = [XHRNetworkingToolManager shareManager];
    
    // 设置为通用格式
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{
                               @"page_size":@15,
                               @"page":@1,
                               @"ci1_id":@1,
                               @"keyword":@""
                               };
    
    [manager POST:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        NSMutableArray *mArr = [NSMutableArray array];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 字典转模型
            XHRSickBranchesModel *model = [XHRSickBranchesModel modelWithDict:obj];
            
            [mArr addObject:model];
            
        }];
        
        if (successBlock) {
            successBlock(mArr.copy);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (errorBlock) {
            errorBlock();
        }
    }];
}

- (NSString *)description{
    return [NSString stringWithFormat:@"%@,%@",[super description],self.ci3_name];
}

// 转拼音
+ (NSString *)getPinyinWithString:(NSString *)string
{
    NSString *pinyin;
    if ([string length]) {
        NSMutableString *ms = [[NSMutableString alloc] initWithString:string];
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
            
        }
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
            
        }
        pinyin = ms;
    }
    return pinyin;
}

+ (NSString *)getFirstLetterWithString:(NSString *)string
{
    NSString *regex = @"^[a-zA-Z]*$";
    NSString *firstLetter = [string substringToIndex:1];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([predicate evaluateWithObject:firstLetter] == YES) {
        return [firstLetter uppercaseString];
    }else {
        return @"~";
    }
}
@end
